package com.ruoyi.business.youdaoController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.services.HttpService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.SaveType;
import com.ruoyi.common.utils.EncrypDES;
import com.ruoyi.common.utils.GetUrlParam;
import com.ruoyi.common.utils.OtherUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.vo.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ownWifiApi")
public class WifiController {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private HttpService httpService;

    private final static String CLASSESEN = "youdao";


    /**
     * wifi -> 抖音
     * http://47.94.2.84:9185/ownWifiApi/rta?channelId=6806750194337792&dmpid=__SID__&imei=__IMEI__&imei_md5=__HASH_IMEI__&idfa=__IDFA__&idfa_md5=__HASH_IDFA__&oaid=__OAID__&oadi_md5=__OAID_MD5__&ts=__STIME__&request_id=__SID__&os=__OS__&ua=__UA__&ip=__IP__&ip_md5=__IP_MD5__&caid=__caid__&android_id_md5=__ANDROID_ID_MD5__&mac_md5=__MAC_MD5__
     * @param request
     * @return
     * @throws Exception
     */
    @Log(title = "有道", businessType = BusinessType.RTA, esIndex = "youdao_log", saveType = SaveType.ES)
    @RequestMapping("/rta")
    public Object rtaSend(HttpServletRequest request) throws Exception {
        String queryString = request.getQueryString();
        String channelId = request.getParameter("channelId");
        String dmpid = request.getParameter("dmpid");

        if(StringUtils.isBlank(channelId) || "null".equals(channelId)){
            return R.failure().msg("channelId不能为空");
        }
        if(StringUtils.isBlank(dmpid) || "null".equals(dmpid)){
            return R.failure().msg("dmpid不能为空");
        }

        Map<String, Object> channelDetail = redisCache.getCacheMapValue(CLASSESEN+"Cache", channelId);
        if(channelDetail == null){
            return R.failure().msg("channelId不正确");
        }
        if("0".equals(channelDetail.get("isopen")+"")){
            return R.failure().msg("channelId未开启");
        }

        Map<String, Object> baseParamsMap = GetUrlParam.queryStrToMap(queryString);


        String ua = baseParamsMap.get("ua") + "";
        String ip = baseParamsMap.get("ip") + "";
        if(StringUtils.isNotBlank(ua) && !"null".equals(ua)){
            ua = URLDecoder.decode(ua, Constants.UTF8);
        }else{
            //request 获取 ua
            ua = request.getHeader("User-Agent");
            baseParamsMap.put("ua", URLEncoder.encode(ua, Constants.UTF8));
        }
        if(StringUtils.isNotBlank(ip) && !"null".equals(ip)){
        }else{
            //request 获取 ip
            ip = IpUtils.getIpAddr(request);
        }


        String url = channelDetail.get("rta_adress") + "";
        String requestAddress = GetUrlParam.getRequestAddress(url);
        String staticParameters = GetUrlParam.TruncateUrlPage(url);
        Map<String, Object> staticParamsMap = GetUrlParam.queryStrToMap(staticParameters);
        baseParamsMap.putAll(staticParamsMap);


        List<String> tempList = new ArrayList<>();
        for(String key: baseParamsMap.keySet()){
            String value = baseParamsMap.get(key) + "";
            tempList.add(key+"="+value);
        }
        String requestParameters = String.join("&", tempList);
        long l = System.currentTimeMillis();
        String reportResultsStr = HttpUtils.sendGet(requestAddress, requestParameters, ip, ua);
        long l1 = System.currentTimeMillis();
        redisCache.setCacheMapValue(dmpid+"_Cache", "reportTimeConsumption", (l1-l)+"");
        try{
            JSONObject jsonObject = JSONObject.parseObject(reportResultsStr);
            String res = jsonObject.getJSONObject("data").getString("res");
            if("ALL".equals(res)){
                return 1;
            }
            return 0;
        }catch (Exception e){
            return 0;
        }
    }



    private String getMapValue(Map<String, Object> map, String key){
        if(map == null){
            return "";
        }
        if(map.get(key) == null){
            return "";
        }
        return map.get(key)+"";
    }



}
